﻿using System;
using System.Collections.Generic;
using System.Windows.Automation;
using System.Windows.Automation.Provider;

namespace AutomationFramework
{
    /// <summary>
    /// A basic implementation of IRawElementSimpleProvider.
    /// Many customizations can be done by overriding virtual methods of this class,
    /// rather than having to implement the whole interface.
    /// </summary>
    [System.Runtime.InteropServices.ComVisible(true)]
    public abstract class ItemProvider : IRawElementProviderSimple
    {
        #region Fields

        private Dictionary<int, object> myAutomationProperties = new Dictionary<int, object>();

        #endregion

        #region Protected Virtual Methods

        /// <summary>
        /// Get the window handle for a provider that is a full HWND
        /// </summary>
        /// <returns></returns>
        protected virtual IntPtr GetWindowHandle()
        {
            return IntPtr.Zero;
        }

        /// <summary>
        /// Get the localized name for this control
        /// </summary>
        /// <returns></returns>
        protected virtual string GetName()
        {
            return null;
        }

        #endregion

        #region Other Protected Methods

        /// <summary>
        /// Add the Automation Properties.
        /// </summary>
        /// <param name="propertyId"></param>
        /// <param name="value"></param>
        protected void AddAutomationProperty(int propertyId, object value)
        {
            this.myAutomationProperties.Add(propertyId, value);
        }

        #endregion

        #region IRawElementProviderSimple Members

        /// <summary>
        /// Retrieves an object that provides support for a control pattern on a UI Automation Element.
        /// </summary>
        /// <param name="patternId"></param>
        /// <returns></returns>
        public virtual object GetPatternProvider(int patternId)
        {
            return null;
        }

        /// <summary>
        /// Retrieves the value of a property supported by the UI Automation provider.
        /// </summary>
        /// <param name="propertyId"></param>
        /// <returns></returns>
        public virtual object GetPropertyValue(int propertyId)
        {
            // Check the static props list first
            if (this.myAutomationProperties.ContainsKey(propertyId))
            {
                return this.myAutomationProperties[propertyId];
            }

            // Switching construct to go get the right property from a virtual method.
            if (propertyId == AutomationElementIdentifiers.NameProperty.Id)
            {
                return GetName();
            }

            // Add further cases here to support more properties. :)
            return null;
        }

        /// <summary>
        /// Gets a base provider for this element.
        /// </summary>
        public IRawElementProviderSimple HostRawElementProvider
        {
            get
            {
                IntPtr hwnd = this.GetWindowHandle();
                if (hwnd != IntPtr.Zero)
                {
                    return AutomationInteropProvider.HostProviderFromHandle(this.GetWindowHandle());
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// Specify the type of UI Automation provider.
        /// </summary>
        public virtual ProviderOptions ProviderOptions
        {
            get { return ProviderOptions.ServerSideProvider; }
        }

        #endregion
    }
}
